package com.ft.admix.gdt.adapter;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.bytedance.sdk.openadsdk.TTRewardVideoAd;
import com.bytedance.sdk.openadsdk.mediation.ad.MediationAdSlot;
import com.ft.admix.ad.MixCustomRewardVideoAdapter;
import com.ft.admix.common.MixConstant;
import com.ft.admix.config.AdConfig;
import com.ft.admix.model.MixRewardItem;
import com.ft.admix.utils.ThreadUtils;

import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

public class RewardAdapterLoader extends MixCustomRewardVideoAdapter {

    TTRewardVideoAd mRewardAd;

    @Override
    protected void loadNetworkAd(Activity context, AdConfig config) {
        if (!TTAdSdk.isSdkReady()) {
            return;
        }
        TTAdNative adNativeLoader = TTAdSdk.getAdManager().createAdNative(context);
        AdSlot adslot = new AdSlot.Builder()
                .setCodeId(config.getAdId())
                .setOrientation(TTAdConstant.VERTICAL)//横竖屏设置
                .setMediationAdSlot(new MediationAdSlot
                        .Builder()
                        .build())
                .build();
        adNativeLoader.loadRewardVideoAd(adslot, new TTAdNative.RewardVideoAdListener() {
            @Override
            public void onError(int i, String s) {
                callLoadFailed(i, s);
            }

            @Override
            public void onRewardVideoAdLoad(TTRewardVideoAd ttRewardVideoAd) {
                if (ttRewardVideoAd != null) {
                    mRewardAd = ttRewardVideoAd;
                    if (isClientBidding()) {

                    } else {
                        callLoadSuccess();
                    }
                    mRewardAd.setRewardAdInteractionListener(new TTRewardVideoAd.RewardAdInteractionListener() {
                        @Override
                        public void onAdShow() {
                            callRewardVideoAdShow();
                        }

                        @Override
                        public void onAdVideoBarClick() {
                            callRewardVideoAdClick();
                        }

                        @Override
                        public void onAdClose() {
                            callRewardVideoAdClose();
                        }

                        @Override
                        public void onVideoComplete() {
                            callRewardVideoComplete();
                        }

                        @Override
                        public void onVideoError() {
                            callRewardVideoError();
                        }

                        @Override
                        public void onRewardVerify(boolean b, int i, String s, int i1, String s1) {
                            callRewardVideoRewardVerify(new MixRewardItem() {
                                @Override
                                public boolean rewardVerify() {
                                    return b;
                                }

                                @Override
                                public float getAmount() {
                                    return i;
                                }

                                @Override
                                public String getRewardName() {
                                    return s;
                                }

                                @Override
                                public Map<String, Object> getCustomData() {
                                    return null;
                                }
                            });
                        }

                        @Override
                        public void onRewardArrived(boolean b, int i, Bundle bundle) {

                        }

                        @Override
                        public void onSkippedVideo() {
                            callRewardVideoAdSkipped();
                        }
                    });
                }


            }

            @Override
            public void onRewardVideoCached() {

            }

            @Override
            public void onRewardVideoCached(TTRewardVideoAd ttRewardVideoAd) {
                RewardAdapterLoader.this.mRewardAd = ttRewardVideoAd;
            }
        });
    }

    @Override
    public MixConstant.AdReadyStatus isReadyCondition() {
        /**
         * 在子线程中进行广告是否可用的判断
         */
        Future<MixConstant.AdReadyStatus> future = ThreadUtils.runOnThreadPool(new Callable<MixConstant.AdReadyStatus>() {
            @Override
            public MixConstant.AdReadyStatus call() throws Exception {
                long currentTime = System.currentTimeMillis();
                if (mRewardAd != null && mRewardAd.getExpirationTimestamp()>currentTime) {
                    return MixConstant.AdReadyStatus.AD_IS_READY;
                } else {
                    return MixConstant.AdReadyStatus.AD_IS_NOT_READY;
                }
            }
        });
        try {
            MixConstant.AdReadyStatus result = future.get(500, TimeUnit.MILLISECONDS);//设置500毫秒的总超时，避免线程阻塞
            if (result != null) {
                return result;
            } else {
                return MixConstant.AdReadyStatus.AD_IS_NOT_READY;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return MixConstant.AdReadyStatus.AD_IS_NOT_READY;
    }

    @Override
    public void show(Activity activity) {
        mRewardAd.showRewardVideoAd(activity);
    }
}
